from flask_jwt_extended import JWTManager
from .models import User, TokenBlocklist
from flask import current_app


jwt = JWTManager()


@jwt.user_identity_loader
def user_identity_lookup(user):
    return user.id


@jwt.user_lookup_loader
def user_lookup_callback(_jwt_header, jwt_data):
    identity = jwt_data["sub"]
    return User.query_by_id(identity)


@jwt.token_in_blocklist_loader
def check_if_token_revoked(jwt_header, jwt_payload):
    jti = jwt_payload["jti"]
    token = TokenBlocklist.query_id_by_jti(jti)
    return token is not None


def init_app(app):
    jwt.init_app(app)
